package com.example.a2205douyin.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.example.a2205douyin.R;
import com.example.a2205douyin.adapter.VideoAdapter;
import com.example.a2205douyin.baseMVVM.view.BaseFragment;
import com.example.a2205douyin.databinding.FragmentXihuanBinding;
import com.example.a2205douyin.entity.VideoEntity;
import com.example.a2205douyin.xihuan.XiHuanViewModel;

import java.util.ArrayList;
import java.util.List;

public class XihuanFragment extends BaseFragment<FragmentXihuanBinding, XiHuanViewModel> {

    List<VideoEntity.DataBean> videos = new ArrayList<>();
    VideoAdapter videoAdapter;

    public XihuanFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initData() {

        viewModel.success.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity entity) {
                ToastUtils.showLong(entity.getMessage());
                if(entity.getCode()==200){
                    videos.addAll(entity.getData());
                    videoAdapter.notifyDataSetChanged();
                }
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });

    }

    @Override
    protected void initView() {
        videoAdapter = new VideoAdapter(R.layout.item_video,videos);
        binding.rv.setAdapter(videoAdapter);
        binding.rv.setLayoutManager(new GridLayoutManager(getContext(),3));

        viewModel.loveVideo();


    }

    @Override
    protected Class<XiHuanViewModel> getViewModel() {
        return XiHuanViewModel.class;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_xihuan;
    }
}